home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / HPPACACH.C < prev    next >
C/C++ Source or Header  |  1991-05-05  |  15KB  |  551 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/microcode/RCS/hppacache.c,v 1.3 1991/05/05 20:26:11 jinx Exp $
  4.  
  5. Copyright (c) 1990-1991 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. #include <stdio.h>
  36. #include <math.h>
  37. #include <sys/stat.h>
  38. #include <memory.h>
  39. #include <nlist.h>
  40.  
  41. #include "hppacache.h"
  42.  
  43. #define true            1
  44. #define false            0
  45. #define boolean            int
  46.  
  47. #ifdef DEBUG
  48.  
  49. #define DEBUGGING(stmt) stmt
  50.  
  51. #else
  52.  
  53. #define DEBUGGING(stmt) do                        \
  54. {                                    \
  55. } while (0)
  56.  
  57. #endif
  58.  
  59. /* File that contains the symbol table for the kernel */
  60.  
  61. #define KERNEL_FILE        "/hp-ux"
  62.  
  63. /* File where the kernel image lives */
  64.  
  65. #define KERNEL_MEMORY_FILE    "/dev/kmem"
  66.  
  67. #define UTSNAME_SYMBOL        "utsname"
  68. #define PDC_CACHE_SYMBOL    "cache_tlb_parms"
  69.  
  70. static struct utsname sysinfo;
  71. static char **the_argv;
  72.  
  73. void
  74. io_error (pname, format, fname)
  75.      char * pname;
  76.      char * format;
  77.      char * fname;
  78. {
  79.   char errmsg[MAXPATHLEN + 257];
  80.   char errstring[MAXPATHLEN + 257];
  81.  
  82.   sprintf (&errmsg[0], format, fname);
  83.   sprintf (&errstring[0], "%s: %s: %s", (the_argv[0]), pname, (&errmsg[0]));
  84.   perror (&errstring[0]);
  85. }
  86.  
  87. void
  88. io_lose (pname, format, fname)
  89.      char * pname;
  90.      char * format;
  91.      char * fname;
  92. {
  93.   io_error (pname, format, fname);
  94.   exit (1);
  95. }
  96.  
  97. struct kernel_locations
  98. {
  99.   long utsname_location;
  100.   long pdc_cache_location;
  101. };
  102.  
  103. struct nlist nl[] =
  104. {
  105.   { UTSNAME_SYMBOL },
  106.   { PDC_CACHE_SYMBOL },
  107.   { 0 },
  108. };
  109.  
  110. void
  111. read_nlist (kloc)
  112.      struct kernel_locations *kloc;
  113. {
  114.   DEBUGGING (printf ("reading nlist...\n"));
  115.  
  116.   if ((nlist (KERNEL_FILE, nl)) != 0)
  117.     io_lose ("read_nlist", "failed on %s", KERNEL_FILE);
  118.  
  119.   DEBUGGING (printf ("reading nlist done.\n"));
  120.  
  121.   kloc->utsname_location = nl[0].n_value;
  122.   kloc->pdc_cache_location = nl[1].n_value;
  123.  
  124.   DEBUGGING (printf ("utsname location = 0x%x\n", kloc->utsname_location));
  125.   DEBUGGING (printf ("pdc_cache location = 0x%x\n", kloc->pdc_cache_location));
  126. }
  127.  
  128. void
  129. read_parameters (pdc_cache)
  130.      struct pdc_cache_dump *pdc_cache;
  131. {
  132.   struct kernel_locations kloc;
  133.   struct utsname kerninfo;
  134.   int kmem = (open (KERNEL_MEMORY_FILE, O_RDONLY));
  135.   if (kmem < 0)
  136.     io_lose ("read_parameters", "open (%s) failed", KERNEL_MEMORY_FILE);
  137.   read_nlist (&kloc);
  138.   if ((lseek (kmem, kloc.utsname_location, 0)) < 0)
  139.     io_lose ("read_parameters", "lseek (%s) failed", KERNEL_MEMORY_FILE);
  140.   if ((read (kmem, (&kerninfo), (sizeof (kerninfo)))) !=
  141.       (sizeof (kerninfo)))
  142.     io_lose ("read_parameters", "read (%s) failed", KERNEL_MEMORY_FILE);
  143.   if ((memcmp ((&kerninfo), (&sysinfo), (sizeof (sysinfo)))) != 0)
  144.     fprintf (stderr, "read_parameters: uname and %s in %s differ.\n",
  145.          kloc.utsname_location, KERNEL_MEMORY_FILE);
  146.   strncpy (pdc_cache->hardware, (kerninfo.machine),
  147.        (sizeof (kerninfo.machine)));
  148.   if ((lseek (kmem, (kloc.pdc_cache_location), 0)) < 0)
  149.     io_lose ("read_parameters", "lseek (%s) failed", KERNEL_MEMORY_FILE);
  150.   if  ((read (kmem, pdc_cache->cache_format,
  151.           (sizeof (pdc_cache->cache_format)))) !=
  152.        (sizeof (pdc_cache->cache_format)))
  153.     io_lose ("read_parameters", "read (%s) failed", KERNEL_MEMORY_FILE);
  154.   if ((close (kmem)) < 0)
  155.     io_lose ("read_parameters", "close (%s) failed", KERNEL_MEMORY_FILE);
  156. }
  157.  
  158. void
  159. print_sel (sel, name, pattern, op)
  160.      unsigned int sel;
  161.      char *name;
  162.      char *pattern;
  163.      char *op;
  164. {
  165.   switch (sel)
  166.   {
  167.     case 0:
  168.       printf ("\n      Both ");
  169.       printf (pattern, "D");
  170.       printf (" and ");
  171.       printf (pattern, "I");
  172.       printf (" must be used to %s.", op);
  173.       break;
  174.  
  175.     case 1:
  176.       printf ("\n      Only ");
  177.       printf (pattern, "D");
  178.       printf (" needs to be used to %s.", op);
  179.       break;
  180.  
  181.     case 2:
  182.       printf ("\n      Only ");
  183.       printf (pattern, "I");
  184.       printf (" needs to be used to %s.", op);
  185.       break;
  186.  
  187.     case 3:
  188.       printf ("\n      Either ");
  189.       printf (pattern, "D");
  190.       printf (" or ");
  191.       printf (pattern, "I");
  192.       printf (" can be used to %s.", op);
  193.       break;
  194.  
  195.     default:
  196.       fprintf (stderr, "\n      Bad %s value %d.", name, (sel));
  197.       break;
  198.   }
  199.  
  200.   return;
  201. }
  202.  
  203. void
  204. print_cst (cst)
  205.      unsigned int cst;
  206. {
  207.   switch (cst)
  208.   {
  209.     case 0:
  210.       printf ("\n      It does not issue coherent operations.");
  211.       break;
  212.  
  213.     case 1:
  214.       printf ("\n      It issues coherent operations.");
  215.       break;
  216.  
  217.     default:
  218.       printf ("\n      It has a reserved cst value %d.", (cst));
  219.       break;
  220.   }
  221.   return;
  222. }
  223.  
  224. void
  225. print_cache (info, name, write_only_p)
  226.      struct cache_info *info;
  227.      char *name;
  228.      boolean write_only_p;
  229. {
  230.   printf ("\n");
  231.  
  232.   /* First print the user-readable information as a comment. */
  233.  
  234.   printf ("    /*\n");
  235.   printf ("      %s-cache information:\n", name);
  236.  
  237.   printf ("\tsize\t\t%ld bytes (%ld K).\n", info->size, (info->size / 1024));
  238.   printf ("\tconf\t\t0x%08lx\n", info->conf.word);
  239.   printf ("\tbase\t\t0x%lx\n", info->base);
  240.   printf ("\tstride\t\t%ld bytes.\n", info->stride);
  241.   printf ("\tcount\t\t%ld entries.\n", info->count);
  242.   printf ("\tloop\t\t%ld association%s per entry.\n",
  243.       info->loop, ((info->loop == 1) ? "" : "s"));
  244.  
  245.   printf ("\tblock size\t%d line%s.\n",
  246.       info->conf.bits.block,
  247.       ((info->conf.bits.block == 1) ? "" : "s"));
  248.   printf ("\tline size\t%d (16-byte units).\n", info->conf.bits.line);
  249.  
  250.   if (write_only_p)
  251.   {
  252.     printf ("      It is a read-only cache.");
  253.   }
  254.   else if (info->conf.bits.wt == 0)
  255.   {
  256.     printf ("      It is a write-to cache.");
  257.   }
  258.   else
  259.   {
  260.     printf ("      It is a write-through cache.");
  261.   }
  262.  
  263.   print_cst ((info->conf.bits.cst));
  264.   print_sel ((info->conf.bits.fsel), "f-sel", "F%sC", "flush");
  265.  
  266.   /* Now print the C-readable information. */
  267.  
  268.   printf ("\n    */\n");
  269.   printf ("    { %ld, 0x%08lx, 0x%lx, %ld, %ld, %ld }",
  270.       info->size, info->conf.word, info->base,
  271.       info->stride, info->count, info->loop);
  272.  
  273.   return;
  274. }
  275.  
  276. void
  277. print_tlb (info, name)
  278.      struct tlb_info *info;
  279.      char *name;
  280. {
  281.   printf ("\n");
  282.  
  283.   /* First print the user-readable information as a comment. */
  284.  
  285.   printf ("    /*\n");
  286.   printf ("      %s-TLB information:\n", name);
  287.  
  288.   printf ("\tsize\t\t%ld entries (%ld K).\n",
  289.       info->size, (info->size / 1024));
  290.   printf ("\tconf\t\t0x%08lx\n", info->conf.word);
  291.   printf ("\tsp_base\t\t0x%lx\n", info->sp_base);
  292.   printf ("\tsp_stride\t%ld\n", info->sp_stride);
  293.   printf ("\tsp_count\t%ld\n", info->sp_count);
  294.   printf ("\toff_base\t0x%lx\n", info->off_base);
  295.   printf ("\toff_stride\t%ld\n", info->off_stride);
  296.   printf ("\toff_count\t%ld\n", info->off_count);
  297.   printf ("\tloop\t\t%ld association%s per entry.",
  298.       info->loop, ((info->loop == 1) ? "" : "s"));
  299.   
  300.   print_cst ((info->conf.bits.cst));
  301.   print_sel ((info->conf.bits.psel), "p-sel", "P%sTLB", "purge");
  302.  
  303.   /* Now print the C-readable information. */
  304.  
  305.   printf ("\n    */\n");
  306.   printf ("    { %ld, 0x%08lx, 0x%lx, %ld, %ld, 0x%lx, %ld, %ld, %ld }",
  307.       info->size, info->conf.word,
  308.       info->sp_base, info->sp_stride, info->sp_count,
  309.       info->off_base, info->off_stride, info->off_count,
  310.       info->loop);
  311.  
  312.   return;  
  313. }
  314.  
  315. void
  316. print_parameters (pdc_cache, node_p)
  317.      struct pdc_cache_dump *pdc_cache;
  318.      int node_p;
  319. {
  320.   struct pdc_cache_result *io_arch_format;
  321.  
  322.   if (node_p)
  323.   {
  324.     printf ("/* Emacs: Use -*- C -*- mode when editting this file. */\n\n");
  325.     printf ("{\n  /* Cache description for %s, an HP PA %s processor. */\n\n",
  326.         sysinfo.nodename,
  327.         sysinfo.machine);
  328.   }
  329.   else
  330.   {
  331.     printf ("{\n");
  332.   }
  333.  
  334.   io_arch_format = ((struct pdc_cache_result *) &(pdc_cache->cache_format));
  335.  
  336.   printf ("  \"%s\",\n\n  {", pdc_cache->hardware);
  337.  
  338.   print_cache (&(io_arch_format->I_info), "I", true);
  339.   printf (",");
  340.   print_cache (&(io_arch_format->D_info), "D", false);
  341.   printf (",");
  342.  
  343.   print_tlb (&(io_arch_format->IT_info), "I");
  344.   printf (",");
  345.   print_tlb (&(io_arch_format->DT_info), "D");
  346.   printf ("\n");
  347.  
  348.   printf ("  }};\n");
  349.   return;
  350. }
  351.  
  352. int
  353. search_pdc_database (fd, pdc_cache, filename)
  354.      int fd;
  355.      struct pdc_cache_dump * pdc_cache;
  356.      char * filename;
  357. {
  358.   while (1)
  359.     {
  360.       int scr =
  361.     (read (fd, ((char *) pdc_cache), (sizeof (struct pdc_cache_dump))));
  362.       if (scr < 0)
  363.     io_lose ("search_pdc_database", "read (%s) failed", filename);
  364.       if (scr != (sizeof (struct pdc_cache_dump)))
  365.     {
  366.       if (scr == 0)
  367.         return (0);
  368.       fprintf (stderr, "%s: %s: incomplete read (%s)\n",
  369.            (the_argv[0]), "search_pdc_database", filename);
  370.       fflush (stderr);
  371.       exit (1);
  372.     }
  373.       if ((strcmp ((sysinfo . machine), (pdc_cache -> hardware))) == 0)
  374.     return (1);
  375.     }
  376. }
  377.  
  378. void
  379. update_pdc_database (pdc_cache, filename)
  380.      struct pdc_cache_dump * pdc_cache;
  381.      char * filename;
  382. {
  383.   int fd = (open (filename, (O_RDWR | O_CREAT), 0666));
  384.   if (fd < 0)
  385.     io_lose ("update_pdc_database", "open (%s) failed", filename);
  386.   if (! (search_pdc_database (fd, pdc_cache, filename)))
  387.     {
  388.       read_parameters (pdc_cache);
  389.       {
  390.     int scr =
  391.       (write (fd, ((char *) pdc_cache), (sizeof (struct pdc_cache_dump))));
  392.     if (scr < 0)
  393.       io_lose ("update_pdc_database", "write (%s) failed", filename);
  394.     if (scr != (sizeof (struct pdc_cache_dump)))
  395.       {
  396.         fprintf (stderr, "%s: %s: incomplete write (%s)\n",
  397.              (the_argv[0]), "update_pdc_database", filename);
  398.         fflush (stderr);
  399.         exit (1);
  400.       }
  401.       }
  402.     }
  403.   if ((close (fd)) < 0)
  404.     io_lose ("update_pdc_database", "close (%s) failed", filename);
  405. }
  406.  
  407. void
  408. print_pdc_database (filename)
  409.      char * filename;
  410. {
  411.   struct pdc_cache_dump pdc_cache_s, *pdc_cache;
  412.   int fd = (open (filename, (O_RDONLY), 0666));
  413.   int first;
  414.  
  415.   if (fd < 0)
  416.     io_lose ("print_pdc_database", "open (%s) failed", filename);
  417.  
  418.   pdc_cache = &pdc_cache_s;
  419.   first = 1;
  420.   while (1)
  421.   {
  422.     int scr =
  423.       (read (fd, ((char *) pdc_cache), (sizeof (struct pdc_cache_dump))));
  424.     if (scr < 0)
  425.       io_lose ("print_pdc_database", "read (%s) failed", filename);
  426.     if (scr != (sizeof (struct pdc_cache_dump)))
  427.     {
  428.       if (scr == 0)
  429.     break;
  430.       fprintf (stderr, "%s: %s: incomplete read (%s)\n",
  431.            (the_argv[0]), "print_pdc_database", filename);
  432.       fflush (stderr);
  433.       exit (1);
  434.     }
  435.  
  436.     if (first == 0)
  437.     {
  438.       putchar ('\f');
  439.       putchar ('\n');
  440.     }
  441.     else
  442.     {
  443.       first = 0;
  444.     }
  445.     print_parameters (pdc_cache, 0);
  446.   }
  447.  
  448.   if ((close (fd)) < 0)
  449.     io_lose ("print_pdc_database", "close (%s) failed", filename);
  450. }
  451.  
  452. void
  453. read_stored_parameters (pdc_cache, filename)
  454.      struct pdc_cache_dump * pdc_cache;
  455.      char * filename;
  456. {
  457.   int fd = (open (filename, (O_RDONLY), 0));
  458.   if (fd < 0)
  459.     io_lose ("read_stored_parameters", "open (%s) failed", filename);
  460.   if (! (search_pdc_database (fd, pdc_cache, filename)))
  461.     {
  462.       fprintf (stderr, "%s: %s: unable to find entry in models database\n",
  463.            (the_argv[0]), "read_stored_parameters");
  464.       fflush (stderr);
  465.       exit (1);
  466.     }
  467.   if ((close (fd)) < 0)
  468.     io_lose ("read_stored_parameters", "close (%s) failed", filename);
  469. }
  470.  
  471. void
  472. verify_parameters (new_pdc_cache, old_pdc_cache)
  473.      struct pdc_cache_dump *new_pdc_cache, *old_pdc_cache;
  474. {
  475.   boolean lose;
  476.  
  477.   lose = false;
  478.   if ((strcmp (new_pdc_cache->hardware, old_pdc_cache->hardware)) != 0)
  479.   {
  480.     lose = true;
  481.     printf ("Model differs: old = %s; current = %s.\n",
  482.         new_pdc_cache->hardware, old_pdc_cache->hardware);
  483.   }
  484.   if ((memcmp (&new_pdc_cache->cache_format,
  485.            &old_pdc_cache->cache_format,
  486.            (sizeof (struct pdc_cache_result)))) != 0)
  487.   {
  488.     lose = true;
  489.     printf ("The stored cache information is incorrect.\n");
  490.   }
  491.   if (!lose)
  492.   {
  493.     printf ("The stored cache information is correct.\n");
  494.   }
  495.   return;
  496. }
  497.  
  498. void
  499. usage ()
  500. {
  501.   fprintf (stderr, "usage: one of:\n");
  502.   fprintf (stderr, " %s -update FILENAME\n");
  503.   fprintf (stderr, " %s -verify FILENAME\n");
  504.   fprintf (stderr, " %s -print FILENAME\n");
  505.   fprintf (stderr, " %s -printall FILENAME\n");
  506.   fflush (stderr);
  507.   exit (1);
  508. }
  509.  
  510. void
  511. main (argc, argv)
  512.      int argc;
  513.      char **argv;
  514. {
  515.   the_argv = argv;
  516.   if ((uname (&sysinfo)) < 0)
  517.     io_lose ("main", "uname failed", 0);
  518.   if (argc != 3)
  519.     usage ();
  520.   {
  521.     char * keyword = (argv[1]);
  522.     char * filename = (argv[2]);
  523.     if ((strcmp (keyword, "-update")) == 0)
  524.       {
  525.     struct pdc_cache_dump pdc_cache;
  526.     update_pdc_database ((&pdc_cache), filename);
  527.       }
  528.     else if ((strcmp (keyword, "-print")) == 0)
  529.       {
  530.     struct pdc_cache_dump pdc_cache;
  531.     update_pdc_database ((&pdc_cache), filename);
  532.     print_parameters (&pdc_cache, 1);
  533.       }
  534.     else if ((strcmp (keyword, "-printall")) == 0)
  535.       {
  536.     print_pdc_database (filename);
  537.       }
  538.     else if ((strcmp (keyword, "-verify")) == 0)
  539.       {
  540.     struct pdc_cache_dump old_pdc_cache;
  541.     struct pdc_cache_dump new_pdc_cache;
  542.     read_stored_parameters ((&old_pdc_cache), filename);
  543.     read_parameters (&new_pdc_cache);
  544.     verify_parameters ((&new_pdc_cache), (&old_pdc_cache));
  545.       }
  546.     else
  547.       usage ();
  548.   }
  549.   exit (0);
  550. }
  551.